package com.app.controller;

import com.app.common.Result;
import com.app.model.UserModel;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import com.app.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.LoginException;


@Controller
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public Result<UserLoginVo> login(@RequestParam(value = "code") String code,
                                     @RequestParam(value = "mobileCode")String mobileCode) throws LoginException {
        //微信登录
        UserModel user = userService.wxLogin(code,mobileCode);
        //生成token
        String token = TokenUtils.sign(user.getUserName());
        UserLoginVo userLoginVO = UserLoginVo.builder()
                .user(user)
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }
    //  /user/save
    @GetMapping("/save")
    public Result save(@RequestParam(value = "faceUrl") String faceUrl,
                       @RequestParam(value = "userName") String userName,
                       @RequestParam(value = "mobile") String mobile){
        UserModel user = userService.save(faceUrl, userName, mobile);
        return Result.success(user);
    }
    //
    @GetMapping("/get")
    public Result<Object> get(@RequestParam(value = "userId") int userId){
        UserModel user = userService.getById(userId);
        return Result.success(user);
    }

    @GetMapping("/selectOpenid")
    public Result<Object> selectOpenid(String code){
        return Result.success(userService.selectOpenid(code));
    }
    @GetMapping("/whiteList")
    public Result<Object> whiteList(){
        return Result.success(userService.whiteList());
    }
}
